// MobileRadioView.cpp : implementation of the CMobileRadioView class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resourceppc.h"

#include "MobileRadioView.h"
#include <atlmisc.h>

#include "include/tinyXML/tinyXML.h"

const char* CONFIGPATH = "Config\\Stations.xml";

BOOL CMobileRadioView::PreTranslateMessage(MSG* pMsg)
{
	if((pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST) &&
	   (pMsg->message < WM_MOUSEFIRST || pMsg->message > WM_MOUSELAST))
		return FALSE;

	HWND hWndCtl = ::GetFocus();
	if(IsChild(hWndCtl))
	{
		// find a direct child of the dialog from the window that has focus
		while(::GetParent(hWndCtl) != m_hWnd)
			hWndCtl = ::GetParent(hWndCtl);

		// give control a chance to translate this message
		if(::SendMessage(hWndCtl, WM_FORWARDMSG, 0, (LPARAM)pMsg) != 0)
			return TRUE;
	}

	return IsDialogMessage(pMsg);
}


LRESULT CMobileRadioView::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// TODO: Add your message handler code here and/or call default
	DoDataExchange(FALSE);
	
	Init();

	return TRUE;
}

LRESULT CMobileRadioView::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// TODO: Add your message handler code here and/or call default
	CleanUp();

	return 0;
}

LRESULT CMobileRadioView::OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// TODO: Add your message handler code here and/or call default


	return 0;
}


LRESULT CMobileRadioView::OnComboCityCbnSelChange(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{
	CString str;
	int sel = m_wndCity.GetCurSel();
	m_wndCity.GetLBText(sel, str);
	m_wndStation.ResetContent();
	 
	std::string city = CT2CA(str);
	unsigned int i = 0;
	for(CityStationMap::iterator it=cityStationMap.find(city);
		it!=cityStationMap.end() && i<cityStationMap.count(city); ++it,++i)
	{
		CString s = it->second->Name.c_str();
		int index = m_wndStation.AddString(s);
		m_wndStation.SetItemData(index, it->second->Id);
	}
	return 0;
}

LRESULT CMobileRadioView::OnComboStationCbnSelChange(WORD wNotifyCode, WORD wID, HWND hWndCtl)
{
	int sel = m_wndStation.GetCurSel();
	int id = (int)m_wndStation.GetItemData(sel);
	//CString image = (GetCurrentPath() + "Image\\" + stationMap[id]->Image).c_str();
	//m_wndPic.SetBitmap(LoadBitmap(NULL, image));
	m_spWMPPlayer->put_URL(CComBSTR(stationMap[id]->Stream.c_str()));
	return 0;
}

void CMobileRadioView::LoadConfig()
{
	std::string p = GetCurrentPath() + std::string(CONFIGPATH);
	TiXmlDocument document = TiXmlDocument(p.c_str());
	if(!document.LoadFile())
	{
		MessageBox(L"Can not open the config file.");
		return;
	}	

	TiXmlHandle docHandle(&document);
	TiXmlElement* cityElement = docHandle.FirstChild("stations").FirstChild("city").Element();
	Station* station;
	std::string city;
	while (cityElement)
	{
		city = cityElement->Attribute("name");
		TiXmlElement* stationElement = cityElement->FirstChildElement("station");
		while (stationElement)
		{
			station = new Station();
			station->City = city;
			station->Id = atoi(stationElement->Attribute("sid"));
			station->Name = stationElement->Attribute("name");
			station->Image = stationElement->Attribute("image");
			station->Stream = stationElement->Attribute("stream");
			station->Website = stationElement->Attribute("website");
			
			stationMap[station->Id] = station;
			cityStationMap.insert(CityStationMap::value_type(station->City, station));
			stationElement = stationElement->NextSiblingElement();
		}
		cityElement = cityElement->NextSiblingElement();
	}
}

void CMobileRadioView::Init()
{
	LoadConfig();

	std::string city;
	for(CityStationMap::iterator it=cityStationMap.begin();
		it!=cityStationMap.end(); ++it)
	{
		if(city.compare(it->first) != 0)
		{
			city = it->first;
			CString c = city.c_str();
			m_wndCity.AddString(c);
		}
	}


	CWindow wndPlaceholder = GetDlgItem ( IDC_WMP );
	CRect rc;
	
    // Get the rect of the placeholder group box, then destroy 
    // that window because we don't need it anymore.
    wndPlaceholder.GetWindowRect ( rc );
    ScreenToClient ( rc );
    wndPlaceholder.DestroyWindow();
 
    // Create the AX host window.
    m_wndWMP.Create ( *this, rc, _T("{6BF52A52-394A-11d3-B153-00C04F79FAA6}"), 
                   WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN );

	
	m_wndWMP.QueryControl(&(m_spWMPPlayer));
	
}

void CMobileRadioView::CleanUp()
{
	if (m_spWMPPlayer)
	{
		m_spWMPPlayer->close();
		m_spWMPPlayer.Release();
	}
	m_wndWMP.Detach();

	for(StationMap::iterator it=stationMap.begin(); 
		it!=stationMap.end(); ++it)
	{
		delete it->second;
	}
}


std::string& CMobileRadioView::GetCurrentPath()
{
	if(path.size() == 0)
	{
		TCHAR pathTChar[MAX_PATH];
		char pathChar[MAX_PATH];
		memset(pathTChar, 0, sizeof(char) * MAX_PATH);
		if (GetModuleFileNameW(NULL,pathTChar,MAX_PATH))
		{
			sprintf(pathChar, "%S", pathTChar);
			path = std::string(pathChar);
			path = path.substr(0, path.find_last_of("\\") + 1);
		}
	}
	return path;
}


